<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="活动编号" prop="activityCode">
        <el-input
          v-model="queryParams.activityCode"
          placeholder="请输入活动编号"
          style="width: 240px"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动名称" prop="activityName">
        <el-input
          v-model="queryParams.activityName"
          placeholder="请输入活动名称"
          style="width: 240px"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['activity:activity:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['activity:activity:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['activity:activity:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['activity:activity:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="activityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="activityId" />-->
      <el-table-column label="序号" align="center" width="50" type="index" />
      <el-table-column label="活动编号" width="250" align="center" prop="activityCode" >
        <template #default="scope">
          <el-link type="primary" @click="getActivityDetail(scope.row)" v-hasPermi="['population:hukou:list']">
            {{ scope.row.activityCode }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="活动宣传图" align="center" prop="activityImage" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.activityImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="活动名称" width="200" align="center" prop="activityName" />
      <el-table-column label="活动地点" align="center" prop="activityLocation" />
      <el-table-column label="活动开始时间" align="center" prop="activityStartTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.activityStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动结束时间" align="center" prop="activityEndTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.activityEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动状态" align="center" prop="activityStatus">
        <template #default="scope">
          <dict-tag :options="activity_status" :value="scope.row.activityStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200px"
                       fixed="right" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['activity:activity:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['activity:activity:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改社区活动对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="activityRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="活动名称" prop="activityName">
          <el-input v-model="form.activityName" placeholder="请输入活动名称" />
        </el-form-item>
        <el-form-item label="活动描述" prop="activityDescription">
          <el-input v-model="form.activityDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="活动地点" prop="activityLocation">
          <el-input v-model="form.activityLocation" placeholder="请输入活动地点" />
        </el-form-item>
        <el-form-item label="活动开始时间" prop="activityStartTime">
          <el-date-picker clearable
            v-model="form.activityStartTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择活动开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动结束时间" prop="activityEndTime">
          <el-date-picker clearable
            v-model="form.activityEndTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择活动结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="组织者姓名" prop="organizerName">
          <el-input v-model="form.organizerName" placeholder="请输入组织者姓名" />
        </el-form-item>
        <el-form-item label="组织者电话" prop="organizerPhone">
          <el-input v-model="form.organizerPhone" placeholder="请输入组织者电话" />
        </el-form-item>
        <el-form-item label="活动状态" prop="activityStatus">
          <el-select v-model="form.activityStatus" placeholder="请选择活动状态">
            <el-option
              v-for="dict in activity_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动图片" prop="activityImage">
          <image-upload v-model="form.activityImage"/>
        </el-form-item>
        <el-form-item label="活动附件" prop="activityFile">
          <file-upload v-model="form.activityFile"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 详情 -->
    <el-dialog title="活动信息详情" v-model="activityDetailOpen" width="800px" append-to-body>
      <el-descriptions border :column="1">
        <el-descriptions-item label="活动编号">{{form.activityCode}}</el-descriptions-item>
        <el-descriptions-item label="活动名称">{{form.activityName}}</el-descriptions-item>
        <el-descriptions-item label="活动起始日期">
          {{ form.activityStartTime }}
        </el-descriptions-item>
        <el-descriptions-item label="活动结束日期">
          {{ form.activityEndTime }}
        </el-descriptions-item>
        <el-descriptions-item label="活动地点">{{ form.activityLocation }}</el-descriptions-item>
        <el-descriptions-item label="活动描述">{{ form.activityDescription }}</el-descriptions-item>
        <el-descriptions-item label="组织者姓名">{{ form.organizerName }}</el-descriptions-item>
        <el-descriptions-item label="组织者电话">{{ form.organizerPhone }}</el-descriptions-item>

        <el-descriptions-item label="活动状态">
          <dict-tag :options="activity_status" :value="form.activityStatus"/>
        </el-descriptions-item>
        <el-descriptions-item label="活动宣传图">
          <image-preview :src="form.activityImage" :width="50" :height="50"/>
        </el-descriptions-item>
        <el-descriptions-item label="活动附件">
          <el-button type="primary" :disabled="!(form.activityFile)" @click="download.resource(form.activityFile)">
            下载
          </el-button>
        </el-descriptions-item>

        <el-descriptions-item label="创建时间">{{ form.createTime }}</el-descriptions-item>
        <el-descriptions-item label="创建人">{{ form.createBy }}</el-descriptions-item>
        <el-descriptions-item label="修改时间">{{ form.updateTime }}</el-descriptions-item>
        <el-descriptions-item label="修改人">{{ form.updateBy }}</el-descriptions-item>
        <el-descriptions-item label="备注" v-if="form.remark && form.remark.trim() !== ''" width="100">
          <el-input v-model="form.remark" type="textarea" :readOnly="true" disabled />
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup name="Activity">
import { listActivity, getActivity, delActivity, addActivity, updateActivity } from "@/api/activity/activity";
import download from "@/plugins/download.js";

const { proxy } = getCurrentInstance();
const { activity_status } = proxy.useDict('activity_status');

const activityList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    activityCode: null,
    activityName: null,
  },
  rules: {
    activityName: [
      { required: true, message: "活动名称不能为空", trigger: "blur" }
    ],
    activityDescription: [
      { required: true, message: "活动描述不能为空", trigger: "blur" }
    ],
    activityLocation: [
      { required: true, message: "活动地点不能为空", trigger: "blur" }
    ],
    activityStartTime: [
      { required: true, message: "活动开始时间不能为空", trigger: "change" }
    ],
    activityEndTime: [
      { required: true, message: "活动结束时间不能为空", trigger: "change" }
    ],
    activityStatus: [
      { required: true, message: "活动状态不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

// 自定义区域
const activityDetailOpen = ref(false);

/** 查询社区活动列表 */
function getList() {
  loading.value = true;
  listActivity(queryParams.value).then(response => {
    activityList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    activityId: null,
    activityCode: null,
    activityName: null,
    activityDescription: null,
    activityLocation: null,
    activityStartTime: null,
    activityEndTime: null,
    organizerName: null,
    organizerPhone: null,
    activityStatus: null,
    activityImage: null,
    activityFile: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null,
    delFlag: null
  };
  proxy.resetForm("activityRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.activityId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加社区活动";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _activityId = row.activityId || ids.value
  getActivity(_activityId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改社区活动";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["activityRef"].validate(valid => {
    if (valid) {
      if (form.value.activityId != null) {
        updateActivity(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addActivity(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _activityIds = row.activityId || ids.value;
  proxy.$modal.confirm('是否确认删除社区活动编号为"' + _activityIds + '"的数据项？').then(function() {
    return delActivity(_activityIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('activity/activity/export', {
    ...queryParams.value
  }, `activity_${new Date().getTime()}.xlsx`)
}

getList();

function getActivityDetail(row) {
  reset();
  const _id = row.activityId;
  getActivity(_id).then(response => {
    form.value = response.data;
    activityDetailOpen.value = true;
  })
}
</script>
